const DEFAULT_LIST_CAPACITY = 10
class List<Item> {
    declare private _size: number
    declare private _arr: Item[]
    constructor(size: number = DEFAULT_LIST_CAPACITY) {
        this._size = size
        this._arr = []
    }
    count(): number {
        return this._arr.length
    }
    get(index: number): Item {
        return this._arr[index]
    }
    set(i: Item): void {
        this._arr.push(i)
    }
}

interface Iterator2<Item> {
    first(): void
    next(): void
    isDone(): boolean
    currentItem(): Item
}

class ListIterator<Item> implements Iterator2<Item> {
    declare private _list: List<Item>
    declare private _current: number
    constructor(alist: List<Item>) {
        this._list = alist
        this._current = 0
    }
    first(): void {
        this._current = 0
    }
    next(): void {
        this._current++
    }
    isDone(): boolean {
        return this._current <= this._list.count()
    }
    currentItem(): Item {
        // if (this.isDone()) {
        //     throw new Error("IteratorOutOfBounds")
        // }
        return this._list.get(this._current)
    }


}

//使用
class Employee {
    declare private _name
    constructor(name: string) {
        this._name = name
    }
    print() {
        console.log(this)
    }
}

const alist = new List<Employee>()
alist.set(new Employee('lisa'))
alist.set(new Employee('lisa2'))
alist.set(new Employee('lisa3'))
alist.set(new Employee('lisa4'))
alist.set(new Employee('lisa5'))
alist.set(new Employee('lisa6'))
const iterator = new ListIterator<Employee>(alist)

function printEmployees(i: ListIterator<Employee>) {
    for (i.first(); i.isDone(); i.next()) {
        // console.log(i.currentItem());
        if (i.currentItem()) {
            i.currentItem().print()
        }

    }
}
printEmployees(iterator)
